using NFreeMarker.Ext.Util;
using NFreeMarker.Template;

namespace NFreeMarker.Ext.Beans
{
    /**
     * Subclass of {@link BeanModel} that exposes the return value of the {@link
     * java.lang.object#toString()} method through the {@link TemplateScalarModel}
     * interface.
     * @author Attila Szegedi
     * @version $Id: StringModel.java,v 1.9 2003/06/03 13:21:33 szegedia Exp $
     */
    public class StringModel : BeanModel, ITemplateScalarModel
    {
        internal static readonly IModelFactory Factory = new StringModelFactory();

        /**
         * Creates a new model that wraps the specified object with BeanModel + scalar
         * functionality.
         * @param object the object to wrap into a model.
         * @param wrapper the {@link BeansWrapper} associated with this model.
         * Every model has to have an associated {@link BeansWrapper} instance. The
         * model gains many attributes from its wrapper, including the caching 
         * behavior, method exposure level, method-over-item shadowing policy etc.
         */
        public StringModel(object obj, BeansWrapper wrapper)
            : base(obj, wrapper)
        {
        }

        /**
         * Returns the result of calling {@link object#toString()} on the wrapped
         * object.
         */
        public string GetAsString()
        {
            return Object.ToString();
        }

        private class StringModelFactory : IModelFactory
        {
            public ITemplateModel Create(object obj, IObjectWrapper wrapper)
            {
                return new StringModel(obj, (BeansWrapper) wrapper);
            }
        }
    }
}